<?php
namespace app\api\model\operation;
use think\facade\Db;

class News{
	
	public function findNewsData($_where,$_field)
	{
		return Db::name("news")->where($_where)->field($_field)->find();
	}
	
	public function selectNewsData($_where,$_field,$_limit,$_order)
	{
		$_data=Db::name("news")->where($_where)->field($_field)->limit(current($_limit),end($_limit))->order($_order)->select();
		return !empty($_data)?$_data->toArray():[];	
	}
	
	public function findNewsComment($_where,$_field,$_user_id){
		return Db::name("news_comment")->alias("c")
			->join("user u","u.id=c.user_id")
			->leftJoin("news_comment_like l","l.comment_id=c.id and l.user_id=".$_user_id)
			->where($_where)->field($_field)->find();
	}
	
	public function selectNewsComment($_where,$_field,$_limit,$_order,$_user_id){
		$_data=Db::name("news_comment")->alias("c")
			->join("user u","u.id=c.user_id")
			->leftJoin("news_comment_like l","l.comment_id=c.id and l.user_id=".$_user_id)
			->where($_where)->field($_field)->limit(current($_limit),end($_limit))->order($_order)->select();
		return !empty($_data)?$_data->toArray():[];	
	}
	
	public function insertData($_insert_data){
		return Db::name("news_comment")->insert($_insert_data,true);
	}
	
	public function setInc($_field,$_number,$_where){
		return Db::name("news")->inc($_field,$_number)->where($_where)->update();
	}
	
	public function insertCommentLike($_insert_data){
		return Db::name("news_comment_like")->insert($_insert_data,true);
	}
	
	public function findCommentLike($_where,$_field)
	{
		return Db::name("news_comment_like")->where($_where)->field($_field)->find();
	}
	
	public function updateCommentLike($_up,$_where){
		return Db::name("news_comment_like")->where($_where)->update($_up);
	}
}
?>